<?php
// $Id$

/**
 * Controller_Departments 控制器
 */
class Controller_Departments extends Controller_Abstract
{

	function actionIndex()
	{
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        $cur_user = $this->_app->currentUserObject();

        if($cur_user->permission_id != 2)
        {
            return $this->_redirect(url('default/index'));
        }

		$page = intval($this->_context->page);
		if ($page < 1) $page = 1;
		$select = Department::find('enterprise_id = ?', $cur_user->enterprise_id)->all();;

		$select->limitPage($page, 10);
		// 将分页信息和查询到的数据传递到视图
		$this->_view['pagination'] = $select->getPagination();
		$this->_view['deps'] = $select->getAll();

		$this->_view['num'] = $select->getCount();
	}

    function actionshowdepuser()
    {
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}
		$cur_user = $this->_app->currentUserObject();
	    $dep = Department::find('department_id = ?', $this->_context->department_id)->query();        
        $select = Userindepartment::find('department_id = ?', $this->_context->department_id)->all();
        $page = intval($this->_context->page);
        if ($page < 1) $page = 1;

        //$select->limitPage($page, 10);

        //$this->_view['pagination'] = $select->getPagination();

        $this->_view['dep'] = $dep;
        $this->_view['depusers'] = $select->getAll();
		$this->_viewname = 'depusers';
    }
    
    function actiondeletemember()
    {
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}
		$cur_user = $this->_app->currentUserObject();

        if(isset($_POST['checkboxid'])){
			$id=$_POST['checkboxid'];
			
			foreach ($id as $value){
				Userindepartment::meta()->destroyWhere('department_id = ? AND user_id = ?', $this->_context->department_id, $value);
			}
		}
        return $this->_redirectMessage(
						'提示您:',
						'部门成员删除成功',
						url('departments/showdepuser', array('department_id' => $this->_context->department_id)));
    }

    function actionaddmember()
    {
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}
		$cur_user = $this->_app->currentUserObject();
	    $dep = Department::find('department_id = ?', $this->_context->department_id)->query();        

        if(isset($_POST['checkboxid']))
        {
			$id=$_POST['checkboxid'];
			
			foreach ($id as $value)
            {
                $dep = Userindepartment::find('user_id = ?', $value)->query();

                if($dep->user_id == $value)
                {  
                    $dep->department_id = $this->_context->department_id;
                    Userindepartment::meta()->destroyWhere('user_id = ?', $value);
                }

                {					
                    $depuser = new Userindepartment();
                    $depuser->department_id = $this->_context->department_id;
                    $depuser->user_id = $value;
                    $depuser->save();
                }
			}
			return $this->_redirectMessage(
						'提示您:',
						'部门添加成员成功',
						url('departments/index'));
		}
        else
        {
            // 分页查询
            $page = intval($this->_context->page);
            if ($page < 1) $page = 1;
            $user_num=0;           

            $select = User::find('enterprise_id = ? AND permission_id <> 1 AND permission_id <> 2', $cur_user->enterprise_id)->all();;
            $select->limitPage($page, 10);

            $this->_view['pagination'] = $select->getPagination();

            $this->_view['users'] = $select->getAll();

            // 将分页信息和查询到的数据传递到视图
            $user_num = $select->getCount();
        }

		$this->_view['dep'] = $dep;
		$this->_viewname = 'addmember';
    }

	function actionAdd()
	{
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}
		$cur_user = $this->_app->currentUserObject();
		
		if(isset($_POST['departmentname']))
		{
			$channelnames = $_POST['departmentname'];//这里取得POST过来的文本框的数目
			//for($i=1011;$i<=($num+1011);$i++)
			foreach($channelnames as $value)
			{ 
			  if($value != "")
			  {
					$dep = new Department();
					$dep->department_name = $value;
					$dep->enterprise_id = $cur_user->enterprise_id;
					$dep->save();
			  }
			}  
			return $this->_redirectMessage(
						'提示您:',
						'部门添加成功',
						url('departments/index'));
		}
	}

	function actionDelete()
	{
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}

		$user = $this->_app->currentUserObject();

        if(isset($_POST['checkboxid'])){
			$id=$_POST['checkboxid'];
			
			foreach ($id as $value){
				//$department = department::find('department_id  = ?', $value)->query();
				//$department->department_hide_status=1;

				Department::meta()->destroyWhere('department_id = ?', $value);
				//删除栏目相关的文章
			}
		}

		return $this->_redirect(url('departments/index'));
	}
	
	function actionEdit()
	{
		if(null == $this->_app->currentUser())
		{
			return $this->_redirect(url('default/index'));
		}

		//$cur_user = $this->_app->currentUserObject();
	    $dep = Department::find('department_id = ?', $this->_context->department_id)->query();
		
		if($dep->id() != $this->_context->department_id)
		{
		    return $this->_redirect(url('department/index'));
		}

        if(isset($_POST['department_title']))
		{
			$dep->department_name =$_POST['department_title'];
			$dep->department_desc =$_POST['department_desc'];
			$dep->save();
			return $this->_redirectMessage(
					'提示您:',
					'编辑成功',
					url('departments/index'));
		}

		$this->_view['dep'] = $dep;
		$this->_viewname = 'edit';
	}

}


